package online.heycm.codegenerator.service.convert;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import online.heycm.codegenerator.common.session.Session;
import online.heycm.codegenerator.generator.GeneratorSetting;
import online.heycm.codegenerator.service.param.FileRes;
import online.heycm.codegenerator.service.param.GenerateReq;

/**
 * @author hey
 * @version 1.0
 * @date 2023/12/21 11:41
 */
public class Convert {

    private Convert() {}

    public static GeneratorSetting convert(Session session, GenerateReq req) {
        GeneratorSetting setting = new GeneratorSetting();
        setting.setDriverClass(session.getDbType().getDriver());
        setting.setUrl(session.getUrl());
        setting.setUsername(session.getUsername());
        setting.setPassword(session.getPassword());
        setting.setOutput(session.getOutput());

        setting.setParentPackage(req.getParentPackage());
        setting.setTables(req.getTables());
        setting.setTablePrefixList(req.getTablePrefixList());
        setting.setFieldPrefixList(req.getFieldPrefixList());
        setting.setInsertUpdateFills(req.getInsertUpdateFills());
        setting.setInsertFills(req.getInsertFills());
        setting.setUpdateFills(req.getUpdateFills());
        setting.setLogicDeleteFill(req.getLogicDeleteFill());
        setting.setEnableLombok(req.isUseLombok());
        setting.setEnableSwagger(req.isUseSwagger2());
        setting.setAuthor(req.getAuthor());

        return setting;
    }

    public static List<FileRes> convertFileRes(List<File> files) {
        List<FileRes> fileRes = new ArrayList<>(files.size());
        int index = 0;
        for (File file : files) {
            FileRes f = new FileRes();
            f.setIndex(index++);
            f.setName(file.getName());
            f.setPath(file.getAbsolutePath());
            fileRes.add(f);
        }
        return fileRes;
    }

}
